package org.abacus.dbtojava;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import org.junit.Test;

public class DBServiceTest {
	DBService dbService = new DBService();
	java.sql.Types s;
	Long l;

	@Test
	public void getCrossReferences() {
		String parentTable = "sys_table";
		String foreignTable = "sys_column";
		List<CrossReference> s = dbService.getCrossReferences(parentTable, foreignTable);
		System.out.println(s);
	}

	@Test
	public void getExportedKeys() {
		String table = "sys_table";
		List<ExportedKey> s = dbService.getExportedKeys(table);
		System.out.println(s);
	}
	@Test
	public void getImportedKeys() {
		String table = "sys_column";
		List<ImportedKey> s = dbService.getImportedKeys(table);
		System.out.println(s);
	}


	@Test
	public void getPrimaryKeys() {
		String tableNamePattern = "sys_user".toUpperCase();
		List<PrimaryKey> s = dbService.getPrimaryKeys(tableNamePattern);
		System.out.println(new String(new char[] { '\0' }).charAt(0));
	}

	@Test
	public void s() {
		BeanFactory beanFactory = BeanFactoryProperties.getInstance(Config.DB_TO_JAVA);
		JdbcFactory jdbcFactory = beanFactory.createBean(JdbcFactory.class);
		Jdbc jdbc = jdbcFactory.createJdbc();
		Connection conn = jdbc.createConnection();
		String s = "ALTER TABLE {tableName} DROP INDEX {indexName}  ; ";

		for (String t : dbService.getTables()) {
			Statement stmt = null;
			try {
				stmt = conn.createStatement();
				stmt.execute(s.replace("{tableName}", t).replace("{indexName}", "`parent_strid`"));
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				if (null != stmt) {
					try {
						stmt.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
			}
			System.out.println(t);
			// for (Column c : dbService.getColumns(t)) {
			// System.out.println(" " + c);
			// }
		}
		try {
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Test
	public void code() {
		BeanFactory beanFactory = BeanFactoryProperties.getInstance(Config.DB_TO_JAVA);
		JdbcFactory jdbcFactory = beanFactory.createBean(JdbcFactory.class);
		Jdbc jdbc = jdbcFactory.createJdbc();
		Connection conn = jdbc.createConnection();

		for (Column c : dbService.getColumns("quality_info")) {
			System.out.println(" new SheetColumn(\"" + c.column_name + "\"," + "\"" + c.column_name + "\"),\n");
		}
		try {
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Test
	public void code1() {
		BeanFactory beanFactory = BeanFactoryProperties.getInstance(Config.DB_TO_JAVA);
		JdbcFactory jdbcFactory = beanFactory.createBean(JdbcFactory.class);
		Jdbc jdbc = jdbcFactory.createJdbc();
		Connection conn = jdbc.createConnection();
		StringBuffer tableNameSb = new StringBuffer();

		String[] tableNames = new String[] { "account", "campaign", "ad_group", "ad", "criterion" };
		for (String tableName : tableNames) {
			String tn = tableName.substring(0, 1).toUpperCase().concat(tableName.substring(1));

			StringBuffer sb = new StringBuffer("public static class " + tn + "Columns  {\n\n");

			for (Column c : dbService.getColumns(tableName)) {
				sb.append(" public static final String " + c.column_name.toUpperCase() + " = \"" + c.column_name.toUpperCase() + "\";\n\n");
			}

			sb.append("\n}");
			tableNameSb.append(sb);
		}
		System.out.println(tableNameSb);
		try {
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Test
	public void code2() {
		BeanFactory beanFactory = BeanFactoryProperties.getInstance(Config.DB_TO_JAVA);
		JdbcFactory jdbcFactory = beanFactory.createBean(JdbcFactory.class);
		Jdbc jdbc = jdbcFactory.createJdbc();
		Connection conn = jdbc.createConnection();
		StringBuffer tableNameSb = new StringBuffer();

		String[] tableNames = new String[] {/*
											 * "account", "campaign",
											 * "ad_group", "ad", "criterion"
											 */"quality_info" };
		for (String tableName : tableNames) {
			String tn = tableName.substring(0, 1).toUpperCase().concat(tableName.substring(1));

			StringBuffer sb = new StringBuffer("public static class " + tn + "Columns  {\n\n");

			for (Column c : dbService.getColumns(tableName)) {
				sb.append(" public static final String " + c.column_name.toUpperCase() + " = \"" + c.column_name.toUpperCase() + "\";\n\n");
			}

			sb.append("\n}");
			tableNameSb.append(sb);
		}
		System.out.println(tableNameSb);
		try {
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Test
	public void spilt(){
		String  s= "<?,?>";
		System.out.println(s.split("\\?").length);
	}
}
